#! /usr/bin/env python
# -*- coding: utf-8 -*-
# android 中 中文英文 翻译对照

from xml.dom.minidom import parse
import xml.dom.minidom
import xlwt


def get_xml_str(xmlPath):
    DOMTree = xml.dom.minidom.parse(xmlPath)
    collection = DOMTree.documentElement
    en_map = {}
    strings = collection.getElementsByTagName("string")
    for item in strings:
        key_ = item.getAttribute("name")
        value_ = item.childNodes[0].data
        en_map[key_] = value_
    string_array = collection.getElementsByTagName("string-array")
    for item in string_array:
        key_ = item.getAttribute("name")
        eles = item.getElementsByTagName("item")
        for index in range(len(eles)):
            item2 = eles[index]
            t = item2.childNodes[0].data
            key__ = "{}*{}".format(key_, index)
            en_map[key__] = t
    return en_map


def write_xls(cn={}, en={}):
    workbook = xlwt.Workbook(encoding='utf-8')
    worksheet = workbook.add_sheet('sheet1')

    keys = cn.keys()
    index = 0
    for key_ in keys:
        worksheet.write(index, 0, label=key_)
        worksheet.write(index, 1, label=cn[key_])
        worksheet.write(index, 2, label=en[key_])
        index += 1

    workbook.save('app_en.xls')


# 使用minidom解析器打开 XML 文档
cn_map = get_xml_str("D:\ProjectGys\\App\\PadSw10\\app\src\\main\\res\\values\\strings.xml")
en_map = get_xml_str("D:\ProjectGys\\App\\PadSw10\\app\src\\main\\res\\values-en\\strings.xml")
write_xls(cn_map, en_map)
